var appPath = getAppPath();


$(function(){
	$("#goodsType").combobox({
		url:appPath+'/system/code_getCodesForComboxNoDefault.do?codeTypeId=1009',
		valueField:'id',
		textField:'text',
		value:""
	});
	$("#freightType").combobox({
		url:appPath+'/system/code_getCodesForComboxNoDefault.do?codeTypeId=1010',
		valueField:'id',
		textField:'text',
		value:""
	});
	$("#delFlg").combobox({
		url:appPath+'/system/code_getCodesForComboxNoDefault.do?codeTypeId=1016',
		valueField:'id',
		textField:'text',
		value:"0"
	});
	$("#dealState").combobox({
		url:appPath+'/system/code_getCodesForComboxNoDefault.do?codeTypeId=1008',
		valueField:'id',
		textField:'text',
		value:""
	});
	initGoodsSourceGrid();
});

function initGoodsSourceGrid(){
	$("#goodsSourceInfoGrid").datagrid({
		url:appPath+'/statistics/goodsSourceInfo_pageList.do',
		height:'full',
		striped: true,
		remoteSort:false,
		singleSelect:true,
		idField:'goodsSourceId',
		queryParams: getQueryParam(),
		columns:[[
		          {field:'goodsSourceId',title:'操作',width:32,align:'center',
					  formatter:function(value,rowData,rowIndex){
			    		  return "<a href='javascript:void(0)'><img width='12px' height='12px' style='border:0px' src='"+appPath+"/images/system/icon/order-search.png' title='查看' onClick='showDetail("+rowIndex+")'/></a>";
					  }
		          },
		          {field:'departure',title:'出发地',width:160,align:'left',sortable:true},
		          {field:'destination',title:'到达地',width:160,align:'left',sortable:true},
		          {field:'goodsName',title:'货物名称',width:120,align:'left',sortable:true},
		          {field:'goodsTypeName',title:'货物类型',width:80,align:'left',sortable:true},
		          {field:'freightTypeName',title:'运输类型 ',width:80,align:'left',sortable:true},
		          {field:'number',title:'数量',width:80,align:'left',sortable:true},
		          {field:'weight',title:'重量',width:80,align:'left',sortable:true},
		          {field:'volume',title:'体积',width:80,align:'left',sortable:true},
		          {field:'freightRate',title:'期望运价',width:80,align:'left',sortable:true},
		          {field:'attentionMatterName',title:'注意事项',width:160,align:'left',sortable:true},
		          {field:'toTime',title:'抵达时间',width:160,align:'left',sortable:true,formatter:fmtToTime},
		          {field:'validPeriod',title:'信息有效期限',width:160,align:'left',sortable:true,formatter:fmtValidPeriod},
		          {field:'remark',title:'备注',width:160,align:'left',sortable:true},
		          {field:'validStateName',title:'有效状态',width:80,align:'left',sortable:true},
		          {field:'dealStateName',title:'交易状态',width:80,align:'left',sortable:true},
		          {field:'scanTimes',title:'浏览次数',width:80,align:'left',sortable:true},
		          {field:'createUser',title:'创建者',width:120,align:'left',sortable:true},
		          {field:'createTime',title:'创建时间',width:120,align:'left',sortable:true,formatter:fmtCreateTime},
		          {field:'updateUser',title:'更新者',width:120,align:'left',sortable:true},
		          {field:'updateTime',title:'更新时间',width:120,align:'left',sortable:true,formatter:fmtUpdateTime},
		          {field:'delFlg',title:'是否删除',width:80,align:'left',sortable:true,formatter:fmtDelFlg}
		        ]],
        pagination:true,
		rownumbers:true,
		onBeforeEdit:function(index,rowData){
			rowData.editing = true;
			$('#goodsSourceInfoGrid').datagrid('refreshRow', index);
		},
		onCancelEdit:function(index,rowData){
			rowData.editing = false;
			$('#goodsSourceInfoGrid').datagrid('refreshRow', index);
		},
		onAfterEdit:function(index,rowData){
			rowData.editing = false;
			$('#goodsSourceInfoGrid').datagrid('refreshRow', index);
		}
	}
	);
	
}


function keyPressQuery(et){
	if (et.keyCode) {
		if (et.keyCode == 13)
			doQuery();
	} else {
		if (et.which == 13)
			doQuery();
    }  
}


/**
 * 展现详细界面
 * @param {} rowData
 */
function showDetail(rowIndex){
	
	//清理内容
	var rowData = $("#goodsSourceInfoGrid").datagrid("getRows")[rowIndex];
	$('#detailForm').form('clear');
	$('#detailForm').form('load',rowData);
	
	$('#detailDlg').window({
			title:'车源信息',
			iconCls:'icon-orderdivide',
			modal: true,
			shadow: false,
			collapsible:false,
			minimizable:false,
			maximizable:false
		});
	$('#detailDlg').window('move',{left:20,top:20});
	$('#detailDlg').window('open');
}


//清除已填查询内容
function doClear(){
	$('#departure').val("");
	$('#destination').val("");
	$('#goodsType').combobox("select","");
	$('#freightType').combobox("select","");
	$('#dealState').combobox("select","");
	$('#delFlg').combobox("select","");
	$('#queryToTimeFrom').datebox("setValue","");
	$('#queryToTimeTo').datebox("setValue","");
	$('#queryValidPeriodFrom').datebox("setValue","");
	$('#queryValidPeriodTo').datebox("setValue","");
}

/**
 * 获取请求信息
 */
function getQueryParam(){
	var queryObject = {};
	queryObject.departure = $("#departure").val();
	queryObject.destination = $("#destination").val();
	queryObject.goodsType = $("#goodsType").combobox("getValue");
	queryObject.freightType = $("#freightType").combobox("getValue");
	queryObject.dealState = $("#dealState").combobox("getValue");
	queryObject.delFlg = $("#delFlg").combobox("getValue");
	queryObject.queryToTimeFrom = $('#queryToTimeFrom').datebox("getValue");
	queryObject.queryToTimeTo = $('#queryToTimeTo').datebox("getValue");
	queryObject.queryValidPeriodFrom = $('#queryValidPeriodFrom').datebox("getValue");
	queryObject.queryValidPeriodTo = $('#queryValidPeriodTo').datebox("getValue");
	
	return queryObject;
}

/**
 * 查询数据
 */
function doQuery(){
	$("#goodsSourceInfoGrid").datagrid('unselectAll');
	$("#goodsSourceInfoGrid").datagrid('load',getQueryParam());
}

function fmtValidPeriod(value, rowData, rowIndex){
	if(!value)return "";
	return rowData.validPeriod=formatDate("y/m/d h:i:s",value);
}
function fmtToTime(value, rowData, rowIndex){
	if(!value)return "";
	return rowData.toTime=formatDate("y/m/d h:i:s",value);
}

function fmtCreateTime(value, rowData, rowIndex){
	if(!value)return "";
	return rowData.createTime=formatDate("y/m/d h:i:s",value);
}

function fmtUpdateTime(value, rowData, rowIndex){
	if(!value)return "";
	return rowData.updateTime=formatDate("y/m/d h:i:s",value);
}

function fmtDelFlg(value, rowData, rowIndex){
	if(value == 0 )return "否";
	else
	  return "是";
}

//时间格式转换
function formatDate(formatStr, fdate)
{
 var fTime, fStr = 'ymdhis';
 if (!formatStr)
  formatStr= "y-m-d h:i:s";
 if (!fdate)
	 return "";
 else
	 fTime = new Date(fdate);
 var formatArr = [
 fTime.getFullYear().toString(),
 (fTime.getMonth()+1).toString(),
 fTime.getDate().toString(),
 fTime.getHours().toString(),
 fTime.getMinutes().toString(),
 fTime.getSeconds().toString() 
 ];
 for (var i=0; i<formatArr.length; i++)
 {
  formatStr = formatStr.replace(fStr.charAt(i), formatArr[i]);
 }
 return formatStr;
}